/* ************************************************************************************************

	Copyright (c) 2005-2006, IGR
	All Rights Reserved.
	Licensed under the Apache License version 2.0 or above.
	For more information on licensing, see:
        http://www.apache.org/licenses/


 ************************************************************************************************
 Objet CHECKBOX
 VERSION:0.85
 DATE:16/03/2006
 ************************************************************************************************
Init parameter :
 Name:               Form name
 top:           	 Top position (in pixel)
 left:               Left position (in pixel)             
 parent:			 Reference of an HTML or a javascript element parent of the checkbox
 text:               Label of the checkBox
 checked             True if checkbox is checked
 className           classname to use for the text
 
*************************************************************************************************/
 
 
// *************************************
// CONSTRUCTOR
// *************************************

rialto.widget.Checkbox=function(name,top,left,parent,text,checked,className){

	//base object parameter
    var objParam=new Object;
  	objParam.name=name;
	objParam.type="checkbox";
    objParam.left=left;
	objParam.top=top;
	
	//base constructor
	this.base = rialto.widget.AbstractComponent;    
    this.base(objParam);

	//release object
	objParam=null;

	//default value
    this.className = "libelle1";	
    this.initChecked = false;        
	this.tabindex=rialto.widget.Form.prototype.tabIndex++;
	this.text=text; 
	//classname
    if (rialto.lang.isString(className)){
		this.className = className;
    }
    //checked
    if (rialto.lang.isBoolean(checked)){
		this.initChecked = checked;
    }

           

    var oThis=this;
    
    // *******************************************
	// GENERATION OF DOM ELEMENTS
	// *******************************************
	
    this.divExt.id = this.id + "_divExt";
    this.divExt.style.position = this.position;
    this.divExt.style.left = this.left + "px";
    this.divExt.style.top = this.top + "px";
    
    // Input
    this.checkbox = document.createElement("INPUT");    
    this.checkbox.type = "checkbox";   
    this.checkbox.id = this.id+ "_Check";
    this.checkbox.name = this.name;
    this.checkbox.tabIndex = this.tabIndex;            
    this.divExt.appendChild(this.checkbox);
    
    //onclick method    
    this.checkbox.onclick=function (){
	    oThis.checked=this.checked;
    	this.value=this.checked;
    	oThis.onclick();
    };
    // Label
    this.lib=new rialto.widget.Label("LIB1",3,20,this.divExt,this.text,this.className,{position:'absolute'});              
    
    //Adapt the width with the text width
    this.divExt.style.width=this.lib.getTailleTexte()+20;    

    //Add to the parent
    if (parent) {this.placeIn(parent);};        
} 

rialto.widget.Checkbox.prototype = new rialto.widget.AbstractComponent;


rialto.widget.Checkbox.prototype.adaptToContext =function(){
   //Just to put the initial check
    this.setCheck(this.initChecked,false); 
}
//******those 2 METHODS ARE DEPRECATED...USE SETENABLE
//to enable
rialto.widget.Checkbox.prototype.active=function() {
	rialto.deprecated('Checkbox','active','setEnable(true)');
    this.setEnable(true);    
}
//to desable
rialto.widget.Checkbox.prototype.inactive=function() {
	rialto.deprecated('Checkbox','inactive','setEnable(false)');
    this.setEnable(false);
}
//******

//this method will allow to enable or disable the checkbox according to the parameter
rialto.widget.Checkbox.prototype.setEnable=function(enable){
	if(enable){
		this.checkbox.disabled=false;
	}
	else{
		this.checkbox.disabled=true;
	}
	this.enable=enable;
}

//******those 2 METHODS ARE DEPRECATED...USE SETCHECK
//to check
rialto.widget.Checkbox.prototype.coche=function(boolC) {
	rialto.deprecated('Checkbox','coche(boolC)','setCheck(true,boolC)');
    this.setCheck(true,boolC);
}

//to uncheck
rialto.widget.Checkbox.prototype.decoche=function(boolC) {
	rialto.deprecated('Checkbox','decoche(boolC)','setCheck(false,boolC)');
    this.setCheck(false,boolC);   
}
//******


//this method will allow to  check and uncheck according to the parameter
rialto.widget.Checkbox.prototype.setCheck=function(check,boolC) {
	this.checkbox.checked = check
    this.checked=check;
    this.checkbox.value=check;
    if (boolC) this.onclick();
}

//return the check status
rialto.widget.Checkbox.prototype.getCheck=function() {
    //return this.checked;
    //better because a form reset doesn't change the checked attribute of the object
    return this.checkbox.checked;
}

rialto.widget.Checkbox.prototype.release=function(){
    this.checkbox.onclick=null;
    this.lib.remove();    
}